﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MLRobotic.Tower.Detection;
using MLRobotic.Tower.Common;

namespace MLRobotic.Tower
{
    public partial class Form1 : Form
    {
        private List<string> _fileNames = new List<string>();
        private int _currentFileIndex = 0;

        private Config _config = new Config();
        private ImageProcessor _processor;

        public Form1()
        {
            _processor = new ImageProcessor(_config);
            InitializeComponent();

            var p1 = new TabPage("Red");
            p1.Controls.Add(new TargetColorControl(_config.Red));
            var p2 = new TabPage("Blue");
            p2.Controls.Add(new TargetColorControl(_config.Blue));
            var p3 = new TabPage("Yellow");
            p3.Controls.Add(new TargetColorControl(_config.Yellow));
            var p4 = new TabPage("Magenta");
            p4.Controls.Add(new TargetColorControl(_config.Magenta));
            var p5 = new TabPage("Green");
            p5.Controls.Add(new TargetColorControl(_config.Green));

            tabColors.TabPages.AddRange(new TabPage[] { p1, p2, p3, p4, p5 });
            rbBlue.Checked = _config.Team == Team.Blue;
            rbRed.Checked = _config.Team == Team.Red;
            txtOffset.Text = _config.CandleOffset.ToString();
            
            Load += new EventHandler(Form1_Load);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            btnLoad_Click(null, null);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(txtDirectory.Text))
            {
                _fileNames.Clear();
                _fileNames.AddRange(Directory.GetFiles(txtDirectory.Text, "*.jpg").OrderBy(s => s));

                _currentFileIndex = 0;
                lblFileCnt.Text = _fileNames.Count.ToString() + " files";
                lblFileNum.Text = (_currentFileIndex + 1).ToString();

                Process();
            }
        }

        public void Process()
        {
            if (_fileNames.Count == 0)
                return;

            string path = _fileNames[_currentFileIndex];
            var image = new Bitmap(path);
            pictSrc.Image = image;
            lblFileName.Text = path;

            _processor.SetImage(image);
            var candles = _processor.FindCandlesBySplittingArea(true);
            candles = _processor.AverageCandlesOnHistory(candles);
            
            pictDest.Image = _processor.LastResult;

            lblCandle1.Text = DisplayCandle(candles, 0);
            lblCandle2.Text = DisplayCandle(candles, 6);

        }

        private string DisplayCandle(bool[] candles, int index)
        {
            StringBuilder sb = new StringBuilder();
            if (_config.Team == Team.Blue)
            {
                sb.Append(candles[index] ? "X " : "_ ");
                sb.Append(candles[index + 1] ? "X " : "_ ");
                sb.Append(candles[index + 2] ? "X " : "_ ");
                sb.Append(candles[index + 3] ? "X " : "_ ");
            }
            else
            {
                sb.Append(candles[index + 3] ? "X " : "_ ");
                sb.Append(candles[index + 2] ? "X " : "_ ");
                sb.Append(candles[index + 1] ? "X " : "_ ");
                sb.Append(candles[index] ? "X " : "_ ");
            }
            return sb.ToString();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            _currentFileIndex--;
            if (_currentFileIndex < 0)
                _currentFileIndex = _fileNames.Count - 1;

            lblFileNum.Text = (_currentFileIndex + 1).ToString();

            Process();
        }

        private void btnNxt_Click(object sender, EventArgs e)
        {
            _currentFileIndex++;
            if (_currentFileIndex >= _fileNames.Count)
                _currentFileIndex = 0;

            lblFileNum.Text = (_currentFileIndex + 1).ToString();

            Process();
        }

        private void rbTeam_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRed.Checked)
                _config.Team = Team.Red;
            else
                _config.Team = Team.Blue;

            Process();
        }

        private void txtOffset_Leave(object sender, EventArgs e)
        {
            if (_config == null)
                return;

            _config.CandleOffset = int.Parse(txtOffset.Text);

            Process();
        }
    }
}
